package com.coollang.smartbra.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.coollang.smartbra.MyAutoLayoutActivity;
import com.coollang.smartbra.R;
import com.coollang.smartbra.adapter.DaySportCharAdapter;
import com.coollang.smartbra.adapter.WeekSportCharAdapter;
import com.coollang.smartbra.model.SportRecordModel;
import com.coollang.smartbra.model.WeekActionModel;
import com.coollang.smartbra.presenter.SportStatisticsActivityPresenter;
import com.coollang.smartbra.utils.DisplayUtils;
import com.coollang.smartbra.utils.TimeUtils;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;
import com.coollang.smartbra.utils.WindowManagerUtils;
import com.coollang.smartbra.views.HorizontalListView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.coollang.smartbra.utils.TimeUtils.secToTime;

/**
 * 运动统计页面
 */
public class SportStatisticsActivity extends MyAutoLayoutActivity {

    private static final String TAG = "SportStatisticsActivity";
    @BindView(R.id.status_bar)
    View statusBar;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.content)
    LinearLayout content;
    @BindView(R.id.rb_male)
    RadioButton mRbMale;
    @BindView(R.id.rb_female)
    RadioButton mRbFemale;
    @BindView(R.id.sex_radio)
    RadioGroup mDayRadio;
    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.day_horizontallistview)
    HorizontalListView mDayHorListView;
    @BindView(R.id.week_horizontallistview)
    HorizontalListView mWeekHorListView;

    @BindView(R.id.tv_item0_0)
    TextView mTvItem0_0;
    @BindView(R.id.tv_item0_1)
    TextView mTvItem0_1;
    @BindView(R.id.tv_item1_0)
    TextView mTvItem1_0;
    @BindView(R.id.tv_item1_1)
    TextView mTvItem1_1;
    @BindView(R.id.tv_item2_0)
    TextView mTvItem2_0;
    @BindView(R.id.tv_item2_1)
    TextView mTvItem2_1;
    @BindView(R.id.tv_item3_0)
    TextView mTvItem3_0;
    @BindView(R.id.tv_item3_1)
    TextView mTvItem3_1;

    private SportStatisticsActivityPresenter mPresenter;
    private String[] mActionTitle = {"", "仰卧起坐", "俯卧撑", "卷腹", "跑步", "计步", "深蹲", "深蹲跳", "平板支撑"};
    private int[] mActionIndex = {0, 33, 35, 34, 11, 21, 31, 32, 41};

    // 当前选择的运动index
    private int mIndex;
    private DaySportCharAdapter mDaySportCharAdapter;

    private List<SportRecordModel.DataBean> mDataBeanList;
    private WeekSportCharAdapter mWeekSportCharAdapter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_sport_statistics);
        ButterKnife.bind(this);
        // 设置沉浸式状态栏
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(statusBar, this, content);
        initIntentData();

        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initListView();

        setDay();
        initRG();
    }

    @Override
    protected void initData() {
        mPresenter = new SportStatisticsActivityPresenter(this);
        mPresenter.requestActionData(mActionIndex[mIndex]);
    }

    private void initIntentData() {
        mIndex = getIntent().getIntExtra("index", 0);
        mTvTitle.setText(mActionTitle[mIndex]);
        mTvTitle.setTextColor(ContextCompat.getColor(this, R.color.black_80));
    }

    private void initListView() {
        mDaySportCharAdapter = new DaySportCharAdapter(this, getResources().getDisplayMetrics().heightPixels - WindowManagerUtils.getStatusBarHeight(this));
        mDayHorListView.setAdapter(mDaySportCharAdapter);
        mDayHorListView.setOnItemClickListener(mDayOnItemClickListener);

        mWeekSportCharAdapter = new WeekSportCharAdapter(this, getResources().getDisplayMetrics().heightPixels - WindowManagerUtils.getStatusBarHeight(this));
        mWeekHorListView.setAdapter(mWeekSportCharAdapter);
        mWeekHorListView.setOnItemClickListener(mWeekOnItemClickListener);
    }

    // 设置周UI
    private void setWeek() {
        // 显示周条目 隐藏天条目
        mDayHorListView.setVisibility(View.GONE);
        mWeekHorListView.setVisibility(View.VISIBLE);

        mRbMale.setBackgroundResource(R.color.tra);
        mRbFemale.setBackgroundResource(R.drawable.shape_yellow_5corners_right);

        // 计算数据
        List<WeekActionModel> weekActionModels = mPresenter.getWeekData(mDataBeanList);

        if (weekActionModels != null && weekActionModels.size() > 0) {
            mWeekSportCharAdapter.notifWeekOrDay(weekActionModels);
            mWeekSportCharAdapter.notifOnItemClick(weekActionModels.size() - 1);
            setWeekBottomUI(weekActionModels.get(weekActionModels.size() - 1));
        }
    }

    /**
     * 设置天UI
     */
    private void setDay() {
        // 显示天条目, 隐藏周条目
        mDayHorListView.setVisibility(View.VISIBLE);
        mWeekHorListView.setVisibility(View.GONE);

        mRbMale.setBackgroundResource(R.drawable.shape_yellow_5corners_left);
        mRbFemale.setBackgroundResource(R.color.tra);

        if (mDataBeanList != null && mDataBeanList.size() > 0) {
            mDaySportCharAdapter.notifOnItemClick(mDataBeanList.size() - 1);
            setDayBottomUI(mDataBeanList.get(mDataBeanList.size() - 1));
        }
    }

    private void initRG() {
        mDayRadio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_male:
                        setDay();
                        break;
                    case R.id.rb_female:
                        setWeek();
                        break;
                    default:
                        break;
                }
            }
        });
    }

    public void updateAdapter(List<SportRecordModel.DataBean> beanList) {
        mDataBeanList = beanList;
        mDaySportCharAdapter.notifWeekOrDay(beanList);
        int i = DisplayUtils.dip2px(this, 30) * (beanList.size() - 1) + DisplayUtils.dip2px(this, 16) * (beanList.size() - 2);
        mDayHorListView.scrollTo(i);

        setDayBottomUI(beanList.get(mDataBeanList.size() - 1));
    }

    private AdapterView.OnItemClickListener mDayOnItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mDaySportCharAdapter.notifOnItemClick(position);
            setDayBottomUI((SportRecordModel.DataBean) mDaySportCharAdapter.getItem(position));
        }
    };
    private AdapterView.OnItemClickListener mWeekOnItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mWeekSportCharAdapter.notifOnItemClick(position);
            setWeekBottomUI((WeekActionModel) mWeekSportCharAdapter.getItem(position));
        }
    };

    private void setDayBottomUI(SportRecordModel.DataBean bean) {
        String tvCount = "";
        int count = bean.getCount();
        if (mActionIndex[mIndex] == 41) {
            // 平板支撑
            tvCount = TimeUtils.secToTime(count);
            mTvItem0_1.setText("支撑时长");

        } else {
            tvCount = count + "";
            mTvItem0_1.setText("个数");
        }
        mTvItem0_0.setText(tvCount);

        if (count != 0) {
            int fault = bean.getFault();
            int sum = fault + count;
            int current = (int) (((float) count / sum) * 100);
            mTvItem1_0.setText(String.valueOf(current));
        } else {
            mTvItem1_0.setText(String.valueOf(0));
        }
        mTvItem1_1.setText("正确率: %");

        mTvItem2_0.setText(secToTime(bean.getDuration()));
        mTvItem2_1.setText("活跃: 分钟");

        mTvItem3_0.setText(String.valueOf(bean.getHeartRate()));
        mTvItem3_1.setText("心率: 次/分钟");
    }

    private void setWeekBottomUI(WeekActionModel model) {
        int currDay = TimeUtils.differentDaysByMillisecond(model.getFirstTimeStamp(), model.getLastTimeStamp());

        String tvCount;
        if (mActionIndex[mIndex] == 41) {
            // 平板支撑
            tvCount = TimeUtils.secToTime(model.getCount() / currDay);
            mTvItem0_1.setText("日均支撑时长");
        } else {
            tvCount = model.getCount() / currDay + "";
            mTvItem0_1.setText("日均个数");
        }
        mTvItem0_0.setText(tvCount);

        mTvItem1_0.setText(currDay + "");
        mTvItem1_1.setText("运动天数");

        mTvItem2_0.setText(secToTime((int) (model.getDuration() / currDay)));
        mTvItem2_1.setText("日均活跃: 分钟");

        mTvItem3_0.setText(model.getHeartRate() / currDay + "");
        mTvItem3_1.setText("平均心率: 次/分钟");
    }
}
